package server;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;
import java.util.logging.SocketHandler;

/**
 * @author sxnan
 * @className RequestProcessor
 * @description TODO
 * @date 2020/7/21
 */
public class RequestProcessor extends Thread{

    private Socket accept;
    private Map<String,HttpServlet> httpServletMap;

    public RequestProcessor(Socket accept, Map<String, HttpServlet> httpServletMap) {
        this.accept = accept;
        this.httpServletMap = httpServletMap;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = accept.getInputStream();
            Request request = new Request(inputStream);
            Response response = new Response(accept.getOutputStream());
            HttpServlet httpServlet = httpServletMap.get(request.getUrl());
            if (httpServlet==null){
                response.outputHtml(request.getUrl());
            }else {
                httpServlet.service(request,response);
            }
            accept.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}